/*
 * Copyright (c) 2017, 2018, sf-express and/or its affiliates. All rights reserved.
 * sf-express PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.ddd.test.exception;

import com.ddd.test.constants.ServiceReturnCode;
import com.ddd.test.i18n.CommonMessageSource;

/**
 * 
 */

public class GlobalExceptionHandler {
	
  public Result<?> toResponse(Exception e) {
	Result<?> result = new Result<>();
	result.setSuccess(false);
    int errorCode = ServiceReturnCode.GLOBAL_ERROR;
    String message = CommonMessageSource.getAccessor().getMessage(String.valueOf(errorCode));
    if (e instanceof BusinessException) {
      errorCode = ((BusinessException) e).getCode();
      message = e.getMessage();
    }

    result.setErrorCode(String.valueOf(errorCode));
    result.setErrorMessage(message);
    return result;
  }
}
